<template>
  <div class="question-page w-1200">
    <div class="left-category">
      <ul>
        <li
          v-for="item in categoryOption"
          :key="item.id"
          :class="{ active: categoryId === item.id }"
          @click="updateCategory(item.id)"
        >
          <img :src="`https://www.runjs.cool/${item.image}`" alt="">
          <span :class="{ 'all-en': !/[\u4E00-\u9FA5]/.test(item.tagName) }">{{ item.tagName }}</span>
        </li>
      </ul>
    </div>
    <div class="center-question-content">
      <a-skeleton :loading="!questionList.length" active>
        <div class="question-list">
          <div class="question-list-item" v-for="item in questionList" :key="item._id">
            <div class="question-title">{{ item.title }}</div>
            <div class="question-level">
              <span class="label">难度：</span>
              <div class="level-detail">
                <i v-for="itemName in Math.floor(item.level)" :key="itemName" class="ri-star-fill"></i>
                <i v-if="item.level % 1 !== 0" class="ri-star-half-fill"></i>
              </div>
            </div>
            <div class="question-type">
              <span>{{ item.category === 'Choice' ? '选择题' : '简答题' }}</span>
              <span>{{ categoryName }}</span>
            </div>
            <nuxt-link class="see-detail" :to="`/question/${item._id}?tagId=${categoryId}`" target="_blank">查看问题详情</nuxt-link>
          </div>
        </div>
      </a-skeleton>
    </div>
    <div class="right-site-info">
      <SiteCopyRight />
    </div>
  </div>
</template>

<script>
import ScrollLoadEvent from '~/util/scrollLoadEvent'
export default {
  name: 'QuestionPage',
  layout: 'BaseLayout',
  async asyncData ({ $axios }) {
    const { data } = await $axios.get('/gimishare/api/v1/question/category')
    return {
      categoryOption: data,
      categoryId: data[0]?.id
    }
  },
  data () {
    return {
      scrollLoadEvent: null,
      page: 1,
      questionList: [],
      hasNext: false
    }
  },

  created () {
    this.findQuestionList()
  },

  mounted () {
    this.scrollLoadEvent = new ScrollLoadEvent(
      this.findQuestionList.bind(this, true)
    )
    this.$nextTick(() => {
      this.scrollLoadEvent.bindScrollEvent()
    })
  },

  methods: {
    async findQuestionList (isScroll) {
      if (isScroll) {
        this.page++
        this.scrollLoadEvent.loadState = false
      }
      const { data } = await this.$axios.get(`/gimishare/api/v1/question?tagId=${this.categoryId}&page=${this.page}`)
      const { list, hasNext } = data
      this.questionList.push(...list)
      this.hasNext = hasNext
      if (isScroll) {
        this.scrollLoadEvent.loadState = hasNext
      }
    },

    updateCategory (categoryId) {
      if (this.categoryId === categoryId) return
      this.scrollLoadEvent.loadState = true
      this.questionList = []
      this.hasNext = false
      this.categoryId = categoryId
      this.page = 1
      this.findQuestionList()
    }
  },

  beforeDestroy () {
    this.scrollLoadEvent.removeScrollEvent()
  },

  computed: {
    categoryName () {
      return this.categoryOption.find(o => o.id === this.categoryId)?.tagName
    }
  }
}
</script>
<style lang="less" scoped>
.question-page {
  margin-top: 12px;
  padding-bottom: 30px;
  display: flex;
  align-items: flex-start;
  .left-category {
    width: 200px;
    background: #fff;
    border-radius: 4px;
    position: fixed;
    top: 72px;
    ul {
      list-style: none;
      margin: 0;
      padding: 12px;
      li {
        height: 40px;
        cursor: pointer;
        padding: 0 12px;
        border-radius: 4px;
        display: flex;
        align-items: center;
        margin: 4px 0;
        span {
          font-size: 14px;
          color: #515767;
          &.all-en {
            font-size: 15px;
          }
        }
        &.active {
          background: var(--select-primary-color);
          color: var(--primary-color);
        }
        &:hover {
          background: var(--alaph-primary-color);
        }
        img {
          width: 18px;
          height: 18px;
          object-fit: cover;
          display: block;
          margin-right: 8px;
          border-radius: 6px;
        }
      }
    }
  }
  .center-question-content {
    padding-left: 200px;
    margin: 0 12px;
    flex: 1;
    width: 0;
    ::v-deep .ant-skeleton-content {
      background-color: #fff;
      padding: 12px 20px;
      border-radius: 4px;
    }
    .question-list {
      &-item {
        padding: 16px 24px;
        background-color: #fff;
        border-radius: 4px;
        & + .question-list-item {
          margin-top: 12px;
        }
        .question-title {
          color: #000;
          font-size: 16px;
        }
        .question-level {
          margin-top: 8px;
          display: flex;
          align-items: center;
          .label {
            color: #999;
          }
          .level-detail {
            display: flex;
            font-size: 0;
            i {
              font-size: 15px;
              color: #f97316;
              margin-right: 2px;
            }
          }
        }
        .question-type {
          margin-top: 8px;
          span {
            background-color: #f2f3f5;
            padding: 3px 6px;
            border-radius: 2px;
            color: #8a919f;
            font-size: 11px;
          }
        }
        .see-detail {
          font-size: 12px;
          color: var(--primary-color);
          margin-top: 12px;
          display: inline-block;
          padding-left: 2px;
        }
      }
    }
  }
  .right-site-info {
    position: sticky;
    top: 73px;
  }
}
</style>
